# Create your views here.
from django.shortcuts import render_to_response
from django.conf.urls.defaults import *
from django.contrib.auth import authenticate, login
from betting.main.models import LoginForm, BankModel, UserProfile, UserBetsModel
from betting.board.models import BetModel
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
import locale

def index(request):
    if request.user.is_authenticated(): 
        if request.method == 'POST':
            allbets = BetModel.objects.all()
            bet_list = list()
            for bet in allbets:
                
                betaction = request.POST.get('bet'+str(bet.id))
                
                if betaction == 'buy' or betaction == 'sell':
                    check_bets = UserBetsModel.objects.filter(user=request.user)
                    existing = check_bets.filter(bet=bet)
                    
                    if existing.count() == 0:
                        bet_list.append(bet)
                        if betaction == 'buy':
                            newbet = UserBetsModel(user=request.user, bet=bet,buy=True,bet_return=0,return_sign=False)
                            bank = BankModel.objects.get(user=request.user)
                            
                            if bank.bankroll - bet.price < 0:
                                bankroll = locale.format("%d",bank.bankroll,grouping=True)
                                bets = BetModel.objects.filter(active=True)
                                bank_error = "Not enough funds for bet"
                                return render_to_response('board.html',{'page':'board','bankroll':bankroll,'bets':bets,'bank_error':bank_error},context_instance=RequestContext(request))
                           
                            bank.bankroll = bank.bankroll - bet.price
                        else:
                            newbet = UserBetsModel(user=request.user, bet=bet,buy=False,bet_return=0,return_sign=False)
                            bank = BankModel.objects.get(user=request.user)
                            if bank.bankroll - (bet.amount-bet.price) < 0:
                                bankroll = locale.format("%d",bank.bankroll,grouping=True)
                                bets = BetModel.objects.filter(active=True)
                                bank_error = "Not enough funds for bet"
                                return render_to_response('board.html',{'page':'board','bankroll':bankroll,'bets':bets,'bank_error':bank_error},context_instance=RequestContext(request))
                            
                            bank.bankroll = bank.bankroll - (bet.amount-bet.price)
                        
                        newbet.price = bet.price
                        newbet.expired = bet.expired
                        newbet.pre_price = 0
                        newbet.save()
                        
                        
                        bank.current_bets += 1
                        bank.total_bets += 1
                        bank.save()
            betcount = len(bet_list)
            return render_to_response('confirm.html',{'betlist':bet_list,'range': range(betcount),'betcount':betcount},context_instance=RequestContext(request))
        else:
            locale.setlocale(locale.LC_ALL, 'en_US')
            bank = BankModel.objects.get(user=request.user)
            bankroll = locale.format("%d",bank.bankroll,grouping=True)
            profile= UserProfile.objects.get(user=request.user)
            
            bets = BetModel.objects.filter(active=True).order_by('expired')
            type = "expired"
            
        return render_to_response('board.html',{'page':'board','bankroll':bankroll,'bets':bets,'type':type},context_instance=RequestContext(request))
    else:
         return HttpResponseRedirect(reverse('betting.main.views.index'))
     
def alpha(request):
    locale.setlocale(locale.LC_ALL, 'en_US')
    bank = BankModel.objects.get(user=request.user)
    bankroll = locale.format("%d",bank.bankroll,grouping=True)
    profile= UserProfile.objects.get(user=request.user)
    
   
    bets = BetModel.objects.filter(active=True).order_by('description')
    type = "alpha"
    
    return render_to_response('board.html',{'page':'board','bankroll':bankroll,'bets':bets,'type':type},context_instance=RequestContext(request))

def price(request):
    locale.setlocale(locale.LC_ALL, 'en_US')
    bank = BankModel.objects.get(user=request.user)
    bankroll = locale.format("%d",bank.bankroll,grouping=True)
    profile= UserProfile.objects.get(user=request.user)
    
   
    bets = BetModel.objects.filter(active=True).order_by('price')
    type = "price"
    
    return render_to_response('board.html',{'page':'board','bankroll':bankroll,'bets':bets,'type':type},context_instance=RequestContext(request))

def category(request):
    locale.setlocale(locale.LC_ALL, 'en_US')
    bank = BankModel.objects.get(user=request.user)
    bankroll = locale.format("%d",bank.bankroll,grouping=True)
    profile= UserProfile.objects.get(user=request.user)
    
   
    bets = BetModel.objects.filter(active=True).order_by('title')
    
    temp = bets[0].title
    cats = []
    cats.append(temp)
    for bet in bets:
        if temp != bet.title:
            cats.append(bet.title)
            temp = bet.title
        
    cat_count = cats.count 
    
    type = "cats"
    
    return render_to_response('board.html',{'page':'board','bankroll':bankroll,'bets':bets,'type':type,'cats':cats},context_instance=RequestContext(request))

